﻿using COC.Debug;
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
namespace COC.Content.Task
{
abstract public class TaskCollection: IEnumerable
		{
			protected Queue<IEnumerator>         registeredEnumerators { get; private set; }
			
			public bool                                                isRunning                 { protected set; get; }
			public int                                                registeredTasks { get { return registeredEnumerators.Count; } }
			
			abstract public                                 float progress { get; }
			
			public TaskCollection()
			{
				registeredEnumerators = new Queue<IEnumerator>();
			}
			
			public void Add(ITask task)
			{
				if (task == null)
					throw new ArgumentNullException();
				
				Add(new AsyncTask(task));
			}
			
			public void Add(IEnumerable enumerable)
			{
				if (enumerable is TaskCollection)
				{
					registeredEnumerators.Enqueue(new EnumeratorWithProgress(enumerable.GetEnumerator(), 
					                                                         () => { return (enumerable as TaskCollection).progress;}));
					
					if ((enumerable as TaskCollection).registeredTasks == 0)
                        DebugTool.LogError("Avoid to Register zero size tasks");
				}
				else
					registeredEnumerators.Enqueue(enumerable.GetEnumerator());
				
				if (enumerable == null)
					throw new ArgumentNullException();
				
				
			}
			
			public void Add(IEnumerator enumerator)
			{
				if (enumerator == null)
					throw new ArgumentNullException();
				
				registeredEnumerators.Enqueue(enumerator);
			}
			
			abstract public IEnumerator GetEnumerator();
		}







	}